package cn.lhz.car.mysql

import cn.lhz.car.util.SparkUtil
import org.apache.spark.sql.{SaveMode, SparkSession}

import java.util.Properties

/**
 * 同步厂商销量分析结果到MySQL
 *
 * @author 李昊哲
 * @version 1.0.0
 */
object ManufacturerSalesSync {
  def main(args: Array[String]): Unit = {
    // 获取 SparkSession 对象
    val sparkSession: SparkSession = SparkUtil()

    // 同步分析结果到mysql
    import sparkSession.sql

    // 同步销量数据
    val df_sales = sql("select * from car_sales.dws_manufacturer_sales")
    var properties: Properties = SparkUtil.mysqlConnectionProperties("car_sales", "manufacturer_sales")
    var url: String = properties.getProperty("url")
    var tableName: String = properties.getProperty("tableName")
    df_sales.write.mode(SaveMode.Overwrite).jdbc(url = url, table = tableName, connectionProperties = properties)

    // 同步销量占比数据
    val df_percent = sql("select * from car_sales.dws_manufacturer_sales_percent")
    properties = SparkUtil.mysqlConnectionProperties("car_sales", "manufacturer_sales_percent")
    url = properties.getProperty("url")
    tableName = properties.getProperty("tableName")
    df_percent.write.mode(SaveMode.Overwrite).jdbc(url = url, table = tableName, connectionProperties = properties)

    sparkSession.stop()
  }
} 